<template>
	<div>
		<h2>公募基本文件</h2>
		<el-table :data="tableData" border style="width: 100%">
			<el-table-column prop="type" label="类型" width="300"> </el-table-column>
			<el-table-column prop="typeName" label="名称" width="500">
				<template slot-scope="scope">
					<div class="texts">
						<div
							v-for="(item, index) in scope.row.typeName"
							:key="index + 'a'"
							class="items"
							:class="{ borderNone: index === scope.row.typeName.length - 1 }"
						>
							<el-popover
								placement="top-start"
								width="200"
								trigger="hover"
								:content="$store.getters.hoverPerty.fundFileInfo[item.objName]"
							>
								<span slot="reference">
									<el-form ref="form" label-width="400px" label-position="left">
										<el-form-item :required="['raiseLogonReplyId', 'foundReplyId'].includes(item.objName)">
											<span slot="label" style="font-weight: 500">
												<span>
													{{ item.title }}
													<span
														style="color: #f56c6c"
														v-if="item.objName === 'raiseLogonReplyId' && isCheck && fileFunc(item)"
													>
														(请上传募集注册批复文件)
													</span>
													<span style="color: #f56c6c" v-if="item.objName === 'foundReplyId' && isCheck && fileFunc(item)">
														(请上传成立批复文件)
													</span>
												</span>
												<span v-if="['6'].includes(dataType)">
													<span
														style="color: red; font-weight: bold; margin-left: 5px"
														v-if="fileCompare(item.objName, `${scope.row.type}-${item.title}`)"
													>
														(未复核)</span
													>
												</span>
											</span>
										</el-form-item>
									</el-form>
								</span>
							</el-popover>
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column prop="typeName" label="操作">
				<template slot-scope="scope">
					<div>
						<div
							v-for="(item, index) in scope.row.typeName"
							:key="index + 'b'"
							class="items"
							:class="{ borderNone: index === scope.row.typeName.length - 1 }"
						>
							<span style="color: red" v-if="item.title === '指数使用协议' && $store.state.fundStep.publicBaseInfo.isIndexFund !== '0'">
								（‘是否指数基金’为‘是’，则可上传文件）</span
							>
							<span
								style="color: red"
								v-else-if="item.title === '交易所无异议函' && $store.state.fundStep.publicBaseInfo.isMarketTrade !== '0'"
								>（‘是否上市交易’为‘是’，则可上传文件）</span
							>
							<span v-else>
								<el-button
									:disabled="isBaseDisabled"
									size="mini"
									type="primary"
									@click="updateShow(item.title)"
									v-show="!['4', '6', '2'].includes(dataType)"
									>点击上传</el-button
								>
							</span>
							<span v-if="'file' in item && Object.keys(item.file).length !== 0" style="margin-left: 16px">
								<template v-if="!!item.file.finalFileId || !!item.file.firstFileId || !!item.file.sealedFileId">
									<el-button size="mini" type="primary" @click="fileClick(item.file, `${item.title}-查看`, '查看')">查看</el-button>
									<el-button size="mini" type="primary" @click="fileClick(item.file, `${item.title}-下载`, '下载')">下载</el-button>
									<el-button
										size="mini"
										type="danger"
										:disabled="isBaseDisabled"
										@click="fileClick(item.file, `${item.title}-删除`, '删除')"
										>删除</el-button
									>
								</template>
							</span>
						</div>
					</div>
				</template>
			</el-table-column>
		</el-table>
		<fileUpdate ref="fileUpdate" :isBaseDisabled="isBaseDisabled" />

		<!-- 上传组件 -->
		<el-dialog :title="title" :visible.sync="addComShow" width="30%" :before-close="cancel" append-to-body>
			<el-form>
				<el-form-item label="生效时间：">
					<el-date-picker
						type="date"
						value-format="yyyy-MM-dd"
						format="yyyy-MM-dd"
						placeholder="选择日期"
						v-model="date1"
						style="width: 100%"
					></el-date-picker>
				</el-form-item>
				<el-form-item label=" ">
					<div class="updateClass">
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:disabled="isBaseDisabled"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click="updateClick('word版')"><i class="el-icon-upload"></i> word版</el-button>
						</el-upload>
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:disabled="isBaseDisabled"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click="updateClick('盖章版')"><i class="el-icon-upload"></i> 盖章版</el-button>
						</el-upload>
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:disabled="isBaseDisabled"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click="updateClick('PDF版')"><i class="el-icon-upload"></i> PDF版</el-button>
						</el-upload>
					</div>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cancel" size="mini" type="primary" style="margin: 2px 20px 0 0">取 消</el-button>
			</span>
		</el-dialog>

		<!-- 操作文件 -->
		<el-dialog :title="titleFile" :visible.sync="addComShowFile" width="20%" :before-close="cancelFile" append-to-body>
			<el-button size="mini" :disabled="!!!fileObj.firstFileId" type="primary" @click.native="fileOperationFunc('word版')"
				><i class="el-icon-upload"></i> word版</el-button
			>
			<el-button size="mini" :disabled="!!!fileObj.sealedFileId" type="primary" @click.native="fileOperationFunc('盖章版')"
				><i class="el-icon-upload"></i> 盖章版</el-button
			>
			<el-button size="mini" :disabled="!!!fileObj.finalFileId" type="primary" @click.native="fileOperationFunc('PDF版')"
				><i class="el-icon-upload"></i> PDF版</el-button
			>
		</el-dialog>
	</div>
</template>
<script>
import { fundFileUpload, downloadFile, openFile } from '@/api/productApi/file'
import { saveFile } from '@/api/productApi/infoMain'
import { fileType, delFileType, dataTree } from './update'
import { fileCompare } from '@/views/infoMain/publicList/components/dataCheck'
import fileUpdate from './fileUpdate' // 文件更新组件
import { jsonArray } from '../rateInfo/rateInfo'
import { FileInfoReview } from '@/views/infoMain/publicList/components/dataReview'
export default {
	components: {
		fileUpdate
	},
	props: {
		setpFunc: {
			type: Function,
			default: Function
		},
		fundSysCode: {
			type: String,
			default: ''
		},
		dataType: {
			type: String,
			default: ''
		}
	},
	data() {
		return {
			fileCompare,
			title: '',
			titleFile: '',
			addComShow: false,
			addComShowFile: false,
			tableData: [
				{
					type: '公募基金募集申请文件',
					typeName: [
						{ title: '承诺函', objName: 'commitmentLetterId' },
						{ title: '特殊承诺函', objName: 'specialLetterId' },
						{ title: '申请报告', objName: 'applyReportId' },
						{ title: '基金合同(申请文件)', objName: 'fundContractId' },
						{ title: '托管协议(申请文件)', objName: 'trustAgreementId' },
						{ title: '招募说明书(申请文件)', objName: 'enlistExplainId' },
						{ title: '内部决议', objName: 'interResultId' },
						{ title: '《法律意见书》', objName: 'lawProposalId' },
						{ title: '《基金产品方案》', objName: 'fundPlanId' },
						{ title: '指数使用协议', objName: 'applyAgreementId' },
						{ title: '交易所无异议函', objName: 'tradeNoActionLetterId' },
						{ title: '《基金合同填报指引》', objName: 'fundContractFillId' },
						{ title: '认购发起份额的承诺函', objName: 'subscribeShareLetterId' },
						{ title: '证监会要求的其他文件.zip', objName: 'otherMaterialsId10' }
					]
				},
				{
					type: '公募基金批复',
					typeName: [
						{ title: '募集注册批复', objName: 'raiseLogonReplyId' },
						{ title: '变更注册批复', objName: 'modifyLogonReplyId' },
						{ title: '延期募集批复', objName: 'delayRaiseReplyId' },
						{ title: '成立批复', objName: 'foundReplyId' }
					]
				},
				{
					type: '公募基金募集文件',
					typeName: [
						{ title: '基金合同(募集文件)', objName: 'fundContractId2' },
						{ title: '基金合同摘要', objName: 'fundContractRemark' },
						{ title: '托管协议(募集文件)', objName: 'trustAgreementId2' },
						{ title: '招募说明书(募集文件)', objName: 'enlistExplainId2' },
						{ title: '招募说明书摘要', objName: 'enlistExplainRemark' },
						{ title: '产品资料概要', objName: 'productDataResume' }
					]
				},
				{
					type: '公募其他文件',
					typeName: [{ title: '期货备忘录', objName: 'otherMaterialsId9' }]
				},
				{
					type: '公募清算文件',
					typeName: [{ title: '清算报告', objName: 'liquInfoationReportId' }]
				}
			],

			fundFileInfo: {},
			date1: new Date(),
			isBaseDisabled: false,
			isCheck: false,
			fileTypeMsg: '', // 文件上传类型
			fileList: [],
			OperationType: '', // 文件操作类型
			fileObj: {} // 操作文件数据对象
		}
	},
	created() {
		this.$bus.$on('publicClearFile', property => {
			if (this.dataType !== '1') {
				if (property in this.fundFileInfo) {
					this.$delete(this.fundFileInfo, property)
				}
				this.tableData.forEach(item => {
					item.typeName.forEach(items => {
						if (items.objName === property && 'file' in items) {
							this.$delete(items, 'file')
						}
					})
				})
			}
		})
	},

	mounted() {},

	methods: {
		fileFunc(obj) {
			if (!obj.file) {
				return true
			} else if (!obj.file.finalFileId && !obj.file.firstFileId && !obj.file.sealedFileId) {
				return true
			} else {
				return false
			}
		},

		// 重新申请将数据集中在store
		updateStoreData() {
			this.$store.state.fundStep.pucliktTotalData.productFundApply.fundFileInfo = this.fundFileInfo
		},

		// 完善信息切换步骤时保存切换前页面数据
		async switchingSteps() {
			this.isCheck = true
			const params = {}
			params.productFundApply = {}
			params.productFundApply.fundFileInfo = this.fundFileInfo
			params.fundRaiseType = '1'
			params.fundSysCode = this.fundSysCode
			await saveFile(params).then(res => {
				if (res.msgCode === 'Y') {
					this.$emit('setpFunc', '1', this.fundSysCode, '', 1)
				}
			})
			return true
		},

		ValidationRequired() {
			return FileInfoReview(this.fundFileInfo)
		},

		// 新增:【保存】和【下一步】
		startDown(type) {
			this.isCheck = true
			this.CustomtextComfirm(() => {
				const params = {}
				params.productFundApply = {}
				params.productFundApply.fundFileInfo = this.fundFileInfo
				params.fundRaiseType = '1'
				params.fundSysCode = this.fundSysCode
				saveFile(params).then(res => {
					if (res.msgCode === 'Y') {
						if (type === '下一步') {
							this.$emit('setpFunc', '1', this.fundSysCode, '', 0)
						} else {
							this.$emit('setpFunc', '1', this.fundSysCode, '', 1)
						}
						this.$message({ type: 'success', message: res.msg })
					}
				})
			}, `${!FileInfoReview(this.fundFileInfo).length ? '此操作将永久提交已编辑数据，是否继续' : `必填项 “ ${FileInfoReview(this.fundFileInfo).join('、')} ” 未填写，是否确认提交`}？`)
		},

		fileClick(obj, title, type) {
			// 操作类型
			if (type) {
				// 文件分类操作
				this.titleFile = title
				this.OperationType = type
				this.fileObj = obj
				this.cancelFile()
			}
		},

		fileDataClear() {
			// 清空操作类型数据
			this.titleFile = ''
			this.OperationType = ''
			this.fileObj = {}
			this.cancelFile()
		},

		// 文件操作分类
		fileOperationFunc(type) {
			let id = ''
			if (type === 'word版') {
				id = this.fileObj.firstFileId
			} else if (type === '盖章版') {
				id = this.fileObj.sealedFileId
			} else if (type === 'PDF版') {
				id = this.fileObj.finalFileId
			}
			if (this.OperationType === '下载') {
				this.downloadFileFunc(id)
			}
			if (this.OperationType === '查看') {
				this.fileSee(id)
			}
			if (this.OperationType === '删除') {
				delFileType(this, this.titleFile.split('-')[0], type)
				this.$message({
					type: 'success',
					message: `${this.titleFile}-${type}-删除成功`
				})
			}
			this.fileDataClear()
		},

		// 文件查看
		fileSee(id) {
			openFile(id)
		},

		// 文件下载
		downloadFileFunc(id) {
			downloadFile({ fid: id }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					this.$message({ showClose: true, message: '正在下载，请稍等......', type: 'success' })
					window.open(`/update/download.do?fid=${id}`)
				}
			})
		},

		// 父组件调用的方法
		isBaseDisabledCom(data, msgShow) {
			this.isBaseDisabled = data
			this.isCheck = msgShow
		},

		// 上传
		toUploadFile(params) {
			fundFileUpload(params).then(res => {
				if (res.msgCode === 'Y') {
					this.$message({
						type: 'success',
						message: `${this.title}-${this.fileTypeMsg}-文件上传成功`
					})
					fileType(this, res.obj)
					this.cancel()
				} else {
					this.$message({ type: 'error', message: res.msgDesc })
					this.cancel()
				}
			})
		},

		cancel() {
			// 上传组件
			this.addComShow = !this.addComShow
			this.fileList = []
			this.date1 = new Date()
		},

		cancelFile() {
			// 操作组件
			this.addComShowFile = !this.addComShowFile
		},

		updateShow(title) {
			// 上传组件显隐
			this.title = title
			this.cancel()
		},

		updateClick(title) {
			// 上传类型
			this.fileTypeMsg = title
		},

		edit(data) {
			const obj = JSON.parse(JSON.stringify(data))
			if (obj.productFundInfo) {
				if (obj.productFundInfo.fundFileInfo) {
					this.fundFileInfo = obj.productFundInfo.fundFileInfo
					dataTree(this)
				}
			}
			if (obj.productFundApply) {
				if (!!obj.productFundApply.fundFileInfo && this.dataType !== '2' && obj.dataStatus !== '1') {
					this.fundFileInfo = obj.productFundApply.fundFileInfo
					dataTree(this)
				}
			}
			this.$refs.fileUpdate.edit(this.fundSysCode)
		},

		getData() {
			// 当切换到文件信息时，使用全局数据联动本页面
			this.$store.state.fundStep.publicBaseInfo = jsonArray(this.$store.state.fundStep.pucliktTotalData.productFundApply.baseInfo)
		}
	}
}
</script>
<style lang="scss" scoped src='./index.scss'>
</style>
